// pages/my/my.js

const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    orderList:[],
    noOrder : false,
    imagePath: '',
    staticVoiceUrl : '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 设置全局图片路径
    this.setData({
      imagePath: app.globalData.imagePath,
      staticVoiceUrl: app.globalData.staticVoiceUrl
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.checkAuthUserInfo('/pages/my/my');
    this.data.page = 1;
    this.getMyOrderList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  onPullDownRefresh: function () {
    this.data.page = 1;
    this.getMyOrderList();
  },
  onReachBottom: function () {
    if (!this.data.noMoreData) {
      this.data.page = this.data.page + 1;
    }
    this.getMyOrderList(true);
  },
  getMyOrderList: function (paged = false) {

    wx.showLoading({
      title: '加载中',
    });

    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'order/my',
      header: {
        'content-type': 'application/json',
        "Cookie": "PHPSESSID=" + wx.getStorageSync('sessionID')
      },
      data: {
        page: that.data.page
      },
      success: function (res) {
        if (res.data.code == 10000 && res.data.message.length > 0) {
          that.setData({
            noOrder: false
          });

          let order_list = [];
          if (paged) {
            order_list = that.data.orderList.concat(res.data.message);
          } else {
            order_list = res.data.message;
          }
          that.setData({
            orderList: order_list
          });
        } else if (res.data.code == 11011){
          app.getSession();
          that.data.page = that.data.page - 1;
          wx.startPullDownRefresh();
        }

        if (that.data.orderList.length <= 0) {
          that.setData({
            noOrder : true
          });
        }
        wx.stopPullDownRefresh();
        wx.hideLoading();

        if (res.data.code == 10000 && res.data.message.length == 0 && that.data.orderList.length > 0) {
          wx.showToast({
            title: '别拉了，真的没有了',
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 跳转至详情页
   */
  toDetail: function (e) {
    var id = e.currentTarget.dataset.key;
    app.formIdSubmit(e);

    wx.navigateTo({
      url: '/pages/detail/detail?id=' + id,
    })
  },
  /**
   * 退出拼单
   */
  saybyeOrder : function(e) {

    app.formIdSubmit(e);

    var that = this;
    var id = e.currentTarget.id;
    wx.showModal({
      title: '提示',
      content: '退出后此拼单将会作废。',
      showCancel: true,
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.apiUrl + 'order/out',
            header: {
              'content-type': 'application/json',
              "Cookie": "PHPSESSID=" + wx.getStorageSync('sessionID')
            },
            data: {
              order_id: id
            },
            success: function (res) {
              if (res.data.code == 10000) {

                wx.startPullDownRefresh();

                wx.showToast({
                  title: '已退出',
                  icon: '',
                  duration: 1000
                });
              }
            },
            fail : function(res) {
              app.reportError(res.errMsg, 'my-saybyeOrder-order/out');
            }
          })
        }
      }
    });
  },
  /**
   * 停止拼单
   */
  stopOrder : function(e) {

    app.formIdSubmit(e);

    var that = this;
    var id = e.currentTarget.id;
    wx.showModal({
      title: '提示',
      content: '停止拼单后将无法重新开启，已参与者将被自动退出。如有拼单需要重新发起拼单。',
      showCancel: true,
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.apiUrl + 'order/stop',
            header: {
              'content-type': 'application/json',
              "Cookie": "PHPSESSID=" + wx.getStorageSync('sessionID')
            },
            data: {
              order_id: id
            },
            success: function (res) {
              if (res.data.code == 10000) {
                wx.startPullDownRefresh();

                wx.showToast({
                  title: '已停止',
                  icon: '',
                  duration: 1000
                });
              }
            },
            fail: function (res) {
              app.reportError(res.errMsg, 'my-stopOrder-order/stop');
            }
            
          });
        }
      }
    });
  },
  modifyOrder : function(e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.showModal({
      title: '提示',
      content: '当前拼单已经有拼友参与，所以只允许修改结束时间和允许参与人数。',
      showCancel: true,
      confirmText: '确定',
      success: function (res) {
        console.log(id);
      }
    });
  }

  /**
   * To Home Page
   */
  , toHome: function (e) {
    app.formIdSubmit(e);
    wx.switchTab({
      url: '/pages/index/index',
    });
  }
  /**
   * To My Page
   */
  , toMy: function (e) {
    app.formIdSubmit(e);
    wx.switchTab({
      url: '/pages/center/center',
    });
  }
})